/*
 * UserNet  --  TCP/IP implementation in userspace
 * Copyright (C) 2010  Robert Millan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef UNET_RPC_H
#define UNET_RPC_H

#include <errno.h>
#include <stdint.h>
#include <netinet/in.h>		/* struct in_addr */

#include "unet/dev.h"
#include "unet/routes.h"
#include "unet/misc.h"

#define UNET_SIGNATURE		(*((uint32_t *) "uNet"))

/* Minimum and maximum ABI versions supported by client & server.  */
#define UNET_MIN_CLIENT_ABI	0
#define UNET_MAX_CLIENT_ABI	0
#define UNET_MIN_SERVER_ABI	0
#define UNET_MAX_SERVER_ABI	0

/* Assert that ABI range is consistent.  */
COMPILE_TIME_ASSERT (UNET_MAX_CLIENT_ABI >= UNET_MIN_CLIENT_ABI);
COMPILE_TIME_ASSERT (UNET_MAX_SERVER_ABI >= UNET_MIN_SERVER_ABI);

/* Assert that ABI ranges of client and server are compatible.  */
COMPILE_TIME_ASSERT (UNET_MAX_CLIENT_ABI >= UNET_MIN_SERVER_ABI);
COMPILE_TIME_ASSERT (UNET_MAX_SERVER_ABI >= UNET_MIN_CLIENT_ABI);

struct unet_abi
{
  uint16_t min;
  uint16_t max;
};

struct unet_rpc_adv
{
  uint32_t signature;
  struct unet_abi server_abi;
};

enum msg_types
  {
    UNET_RPC_INIT,		/* Client requests that this channel is
				   initialized for packet receival.  */
    UNET_RPC_PORT,		/* Client requests operation on a port.  */
    UNET_RPC_COUNT_DEVICES,
    UNET_RPC_GET_DEVICE_INFO, UNET_RPC_SET_DEVICE_INFO,
				/* Used by ifconfig.  */
    UNET_RPC_ROUTE_COUNT,
    UNET_RPC_ROUTE_GET,
    UNET_RPC_ROUTE_ADD,
    UNET_RPC_ROUTE_DEL,		/* Used by route.  */
  };

struct unet_rpc_req
{
  uint32_t signature;
  uint16_t abi;
  int type;
  union
  {
    /* Client requests operation on a port.  */
    struct
    {
      uint8_t op;               /* open, close */
      uint8_t type;             /* TCP, UDP */
      uint16_t num;             /* Port number, 0 means an arbitrary port is requested.  */
      struct in_addr listen_addr;
    } port;

    /* Client requests information relative to a device.  */
    struct
    {
      uint32_t id;
    } get_device_info;

    /* Client requests modification of settings for a device.  */
    struct
    {
      struct unet_device device;
    } set_device_info;

    /* Client requests that route is added or deleted.  */
    struct unet_route route;

    /* UNET_RPC_ROUTE_GET */
    uint32_t route_get;
  } data;
};


enum port_ops
  {
    UNET_RPC_PORT_OPEN,             /* Open specified port.  */
    UNET_RPC_PORT_CLOSE,            /* Close specified port.  */
  };

/* Generic acknowledge with status code.  */
struct unet_rpc_ack
{
  typeof (errno) status;
};

/* Server acknowledges a port operation request.  */
struct unet_rpc_port_ack
{
  typeof (errno) status;
  uint16_t num;             /* Port number in case an arbitrary port was requested and
			       succesfully assigned, undefined otherwise.  */
};

/* Server acknowledges a device/route count request.  */
struct unet_rpc_count_ack
{
  typeof (errno) status;
  uint32_t count;
};

struct unet_rpc_get_device_info_ack
{
  typeof (errno) status;
  struct unet_device device;
};

struct unet_rpc_route_get_ack
{
  typeof (errno) status;
  struct unet_route route;
};

#ifdef _UNET_CLIENT
/* For the client.  */
extern int unet_rpc_init ()  __attribute__ ((warn_unused_result));
extern void unet_rpc_fini ();

extern int unet_rpc_ip_send_packet (const uint8_t *packet, const uint32_t len);
extern int unet_rpc_port (uint8_t type, uint8_t op, struct in_addr listen_addr, uint16_t *num);

extern int unet_rpc_count_devices ();
extern int unet_rpc_get_device_info (unsigned int id, struct unet_device *device) __attribute__ ((warn_unused_result));
extern int unet_rpc_set_device_info (const struct unet_device *device) __attribute__ ((warn_unused_result));

extern int unet_rpc_route_add (const struct unet_route *route) __attribute__ ((warn_unused_result));
extern int unet_rpc_route_del (const struct unet_route *route) __attribute__ ((warn_unused_result));
extern int unet_rpc_route_count () __attribute__ ((warn_unused_result));
extern int unet_rpc_route_get (const uint32_t id, struct unet_route *route) __attribute__ ((warn_unused_result));
#endif

#ifdef _UNET_SERVER
/* For the server.  */
extern int unet_port (int fd, pid_t pid, uid_t uid, uint8_t type, uint8_t op, struct in_addr listen_addr, uint16_t *num);
extern void unet_rpc_ip_incoming_packet (const uint8_t *packet, const uint32_t len);
#endif

#endif
